package com.lagou.rabbit.controller;

import com.lagou.rabbit.config.RabbitMqConfig;
import com.lagou.rabbit.po.Order;
import com.lagou.rabbit.util.OrderUtil;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.core.MessagePropertiesBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.util.UUID;

/**
 * created by wq at 2020/12/13
 */
@RestController
@RequestMapping("/delayPayment")
public class OrderController {

    @Autowired
    private AmqpTemplate amqpTemplate;

    @PostMapping("/orders")
    public String placeOrder() throws UnsupportedEncodingException {
        // 模拟执行生成订单业务
        Order order = new Order();
        order.setId(UUID.randomUUID().toString());
        order.setStatus("未支付");
        OrderUtil.add(order);

        // 生成订单消息
        Message message = MessageBuilder
                .withBody(order.getId().getBytes("utf-8"))
                .build();
        amqpTemplate.convertAndSend(RabbitMqConfig.orderExchange,
                RabbitMqConfig.orderRoutingKey,
                message);

        return order.getId();
    }
}
